package com.hnit.applet.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class ExcelUtils {

    public static CellStyle borderedStyle(Sheet sheet) {
        // 单元格样式，实线边框
        CellStyle style = sheet.getWorkbook().createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        return style;
    }

    public static void writeRow(Sheet sheet, List<Object> values) {
        writeRow(sheet, sheet.getLastRowNum() + 1, values);
    }


    public static void writeRow(Sheet sheet, int rowNum, List<Object> values) {
        CellStyle style = borderedStyle(sheet);
        writeRow(sheet, style, rowNum, values);
    }

    public static void writeRow(Sheet sheet, CellStyle style, int rowNum, List<Object> values) {
        // 行对象，如果没有就创建一个
        Row row = sheet.getRow(rowNum);
        if (row == null) {
            row = sheet.createRow(rowNum);
        }
        // 写入所有值
        for (int i = 0; i < values.size(); i++) {
            Object value = values.get(i);
            writeCell(sheet, row, style, i, value);
        }
    }

    private static void writeCell(Sheet sheet, Row row, CellStyle titleStyle, int i, Object value) {
        Cell cell = row.createCell(i);
        String stringOfValue = null;
        /*if (value instanceof Date) {
            cell.setCellValue((Date) value);
        } else {

        }*/
        if (value != null) {
            stringOfValue = value.toString();
        }
        cell.setCellValue(stringOfValue);
        int cellWidth = sheet.getColumnWidth(i);
        if (StringUtils.isNotBlank(stringOfValue)) {
            // 设置宽度
            cellWidth = Math.max(cellWidth, (stringOfValue.length() + 1) * 512);
//            sheet.setColumnWidth(i, cellWidth);
            if (cellWidth < 255 * 256) {
                sheet.setColumnWidth(i, cellWidth);
            } else {
                sheet.setColumnWidth(i, 6000);
            }
        }
        cell.setCellStyle(titleStyle);
    }

    public static void writeToResponse(Workbook wb, String name, HttpServletResponse response) throws IOException {
        OutputStream os = response.getOutputStream();
        try {
            response.reset();
            response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.addHeader("Content-Disposition", "attachment; filename=\"" + new String(name.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1) + ".xlsx\"");

            wb.write(os);
        } finally {
            if (null != wb) {
                wb.close();
            }
            if (null != os) {
                os.close();
            }
        }

    }
}
